package it.yuzuojian.com.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import it.yuzuojian.com.domain.CommonResult;
import it.yuzuojian.com.domain.Song;
import it.yuzuojian.com.domain.SongList;
import it.yuzuojian.com.service.SongListService;
import it.yuzuojian.com.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/songList")
public class SongListController {
    @Autowired
    private SongListService songListService;

    @PostMapping("/delete")
    public CommonResult deleteMoreSongList(@RequestBody String requestJson) {
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(requestJson);
        JsonNode jsonNode = null;
        try {
            jsonNode = objectMapper.readTree(requestJson);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return new CommonResult<Boolean>(0, "json后端转换失败！", false);
        }
        JsonNode ids = jsonNode.get("ids");
        String[] split = ids.toString().replace("[", "").replace("]", "")
                .trim().split(",");
        Integer[] data = new Integer[split.length];
        for (int i = 0; i < split.length; i++) {
            data[i] = Integer.parseInt(split[i]);
        }
        if (songListService.delete(data)) {
            return new CommonResult<Boolean>(1, "删除成功！", true);
        }
        return new CommonResult<Boolean>(0, "存在删除失败！", false);
    }

    @PostMapping("/update")
    public CommonResult updateSongList(@RequestBody String requestJson) {
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(requestJson);
        SongList songList;
        try {
            songList = objectMapper.readValue(requestJson, SongList.class);
            System.out.println(songList);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return new CommonResult<Boolean>(0, "json后端转换失败！", false);
        }
        if (songListService.update(songList)) {
            System.out.println("修改成功");
            return new CommonResult<Boolean>(1, "修改成功！", true);
        }
        System.out.println("修改失败");
        return new CommonResult<Boolean>(0, "修改失败！", false);
    }

    /*    @PostMapping("/add")
        public CommonResult addSongList(SongList songList, MultipartFile file){
            if(file==null||file.isEmpty()){
                return new  CommonResult<Boolean>(0,"上传文件为空",false);
            }
            String fileName=System.currentTimeMillis()+file.getOriginalFilename();
            String path =System.getProperty("user.dir")+System.getProperty("file.separator")+"music"
                    +System.getProperty("file.separator")+"img"+System.getProperty("file.separator")
                    +"songListPic";
            File file1=new File(path);
            if(file1.exists()){
                file1.mkdir();
            }
            songList.setPic("/img/singListPic"+fileName);
            File upload= new File(path,fileName);

            try {
                file.transferTo(upload);
                if(songListService.insert(songList)){
                    return new CommonResult<Boolean>(1,"添加成功！",true);
                }else {
                    return new CommonResult<Boolean>(0,"添加失败！",false);
                }
            } catch (IOException e) {
                e.printStackTrace();
                return new CommonResult<Boolean>(0,"文件上传失败！",false);
            }
        }*/
    @PostMapping("/add")
    public CommonResult addSongList(@RequestBody String requestJson) {
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(requestJson);
        SongList songList;
        try {
            songList = objectMapper.readValue(requestJson, SongList.class);
            System.out.println(songList);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return new CommonResult<Boolean>(0, "json后端转换失败！", false);
        }
        if (songListService.insert(songList)) {
            return new CommonResult<Boolean>(1, "修改成功！", true);
        }
        return new CommonResult<Boolean>(0, "修改失败！", false);
    }
    @GetMapping("/findByTitle")
    public CommonResult findByTitle(String title) {
        List<SongList> songLists = null;
        System.out.println(title);
        try {
            songLists = songListService.selectByTitle(title);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new CommonResult<List<SongList>>(0, "查询失败！", songLists);
        }
        return new CommonResult<List<SongList>>(1, "查询成功！", songLists);
    }

    @GetMapping("/findByLikeTitle")
    public CommonResult findByLikeTitle(String title) {
        List<SongList> songLists = null;
        System.out.println(title);
        try {
            songLists = songListService.selectByLikeTitle(title);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new CommonResult<List<SongList>>(0, "查询失败！", songLists);
        }
        return new CommonResult<List<SongList>>(1, "查询成功！", songLists);
    }

    @GetMapping("/findByStyle")
    public CommonResult findByStyle(String Style) {
        List<SongList> songLists = null;
        System.out.println(Style);
        try {
            songLists = songListService.selectByTitle(Style);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new CommonResult<List<SongList>>(0, "查询失败！", songLists);
        }
        return new CommonResult<List<SongList>>(1, "查询成功！", songLists);
    }

    @GetMapping("/findByLikeStyle")
    public CommonResult findByLikeStyle(String Style) {
        List<SongList> songLists = null;
        System.out.println(Style);
        try {
            songLists = songListService.selectByLikeStyle(Style);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new CommonResult<List<SongList>>(0, "查询失败！", songLists);
        }
        return new CommonResult<List<SongList>>(1, "查询成功！", songLists);
    }

    @GetMapping("/findAll")
    public CommonResult findAll() {

        List<SongList> songLists = null;
        try {
            songLists = songListService.selectAll();
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new CommonResult<List<SongList>>(0, "查询失败！", songLists);
        }
        return new CommonResult<List<SongList>>(1, "查询成功！", songLists);
    }

    @GetMapping("/findById")
    public CommonResult findById(Integer id) {
        SongList songList = null;
        System.out.println(id);
        try {
            songList = songListService.selectById(id);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new CommonResult<SongList>(0, "查询失败！", songList);
        }
        return new CommonResult<SongList>(1, "查询成功！", songList);
    }

    @PostMapping("/updateSongListPic")
    public CommonResult updateSongListPic(MultipartFile file, Integer id) throws IOException {
        if (file == null || file.isEmpty()) {
            return new CommonResult<Boolean>(0, "上传文件为空", false);
        }
        String fileName = System.currentTimeMillis() + file.getOriginalFilename();
        String path = System.getProperty("user.dir") + System.getProperty("file.separator") + "music"
                + System.getProperty("file.separator") + "img" + System.getProperty("file.separator")
                + "songListPic";
        File file1 = new File(path);
        if (file1.exists()) {
            file1.mkdir();
        }
        File upload = new File(path, fileName);
        System.out.println("path = " + path);
        System.out.println("file:" + upload.getCanonicalPath());
        String pic = "/img/songListPic/" + fileName;
        try {
            file.transferTo(upload);
            SongList songList = new SongList();
            songList.setId(id);
            songList.setPic(pic);
            songListService.update(songList);
        } catch (IOException e) {
            e.printStackTrace();
            return new CommonResult<Boolean>(0, "文件上传失败", false);
        }
        return new CommonResult<Boolean>(1, "文件上传成功", true);

    }


}
